from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from database.db import Base
from .task_scenario_detection_target import task_scenario_detection_targets


class TaskScenario(Base):
    """任务场景表"""
    __tablename__ = "task_scenarios"
    
    id = Column(Integer, primary_key=True, index=True, comment="场景ID")
    name = Column(String(200), nullable=False, comment="场景名称")
    description = Column(Text, comment="场景描述")
    scenario_type = Column(String(50), nullable=False, default="custom", comment="场景类型：preset(预设模板)/custom(自定义)")
    drone_type_requirements = Column(Text, comment="适用无人机类型要求，JSON格式存储")
    detection_targets = Column(Text, comment="检测目标，JSON格式存储")
    default_route_logic = Column(Text, comment="默认航线逻辑配置，JSON格式存储")
    detection_model_ids = Column(Text, comment="关联的检测模型ID列表，JSON格式存储")
    permission_config = Column(Text, comment="人员权限配置，JSON格式存储")
    created_from_template_id = Column(Integer, ForeignKey('task_scenarios.id'), nullable=True, comment="创建自哪个模板ID")
    is_active = Column(Boolean, default=True, comment="是否激活")
    created_at = Column(DateTime, default=datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
    
    # 关系
    created_from_template = relationship("TaskScenario", remote_side=[id], backref="derived_scenarios")
    detection_target_objects = relationship("DetectionTarget", secondary=task_scenario_detection_targets, back_populates="task_scenarios")
    
    def __repr__(self):
        return f"<TaskScenario(id={self.id}, name='{self.name}', type='{self.scenario_type}')>"